package com.atguigu.sink;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.serialization.SimpleStringEncoder;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.connector.source.util.ratelimit.RateLimiterStrategy;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.configuration.MemorySize;
import org.apache.flink.connector.datagen.source.DataGeneratorSource;
import org.apache.flink.connector.datagen.source.GeneratorFunction;
import org.apache.flink.connector.file.sink.FileSink;
import org.apache.flink.core.fs.Path;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.sink.filesystem.OutputFileConfig;
import org.apache.flink.streaming.api.functions.sink.filesystem.RollingPolicy;
import org.apache.flink.streaming.api.functions.sink.filesystem.bucketassigners.DateTimeBucketAssigner;
import org.apache.flink.streaming.api.functions.sink.filesystem.rollingpolicies.DefaultRollingPolicy;

import java.time.Duration;
import java.time.ZoneId;

/**
 * @author yhm
 * @create 2024-04-03 10:43
 */
public class Test01_FileSink {
    public static void main(String[] args) throws Exception {
        // 1. 创建环境
        Configuration conf = new Configuration();
        conf.setInteger("rest.port",8081);

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        // 2. 读取数据源
        DataGeneratorSource<String> dataGenSource = new DataGeneratorSource<>(new GeneratorFunction<Long, String>() {
            @Override
            public String map(Long value) throws Exception {
                // value就是一个计数器
                return "hello:" + value;
            }
        }, Integer.MAX_VALUE, RateLimiterStrategy.perSecond(500), Types.STRING);
        DataStreamSource<String> dataStreamSource = env.fromSource(dataGenSource, WatermarkStrategy.noWatermarks(), "gen_source");

        // 3. 处理数据

        // 4. 输出
        FileSink<String> fileSink = FileSink
                // 输出地址和输出编码
                .<String>forRowFormat(new Path("output"), new SimpleStringEncoder<>())
                // 控制文件夹创建  1h创建一个新的
                .withBucketAssigner(new DateTimeBucketAssigner<>("yyyy-MM-dd HH", ZoneId.systemDefault()))
                // 控制文件的滚动事件
                .withRollingPolicy(DefaultRollingPolicy.builder()
                        .withRolloverInterval(Duration.ofMinutes(1L))// 1min滚动
                        .withMaxPartSize(MemorySize.ofMebiBytes(1))//1m大小滚动
                        .build())
                // 添加前缀和后缀
                .withOutputFileConfig(new OutputFileConfig("atguigu-", ".log")).build();

        dataStreamSource.sinkTo(fileSink);


        // 5. 执行环境
        env.execute();
    }
}
